package com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wanglibao.woodtrade.woodtrade.R;

/**
 * Created by ybli on 2015/8/11.
 */
public class HomepageBannerViewPager {

	private List<View> homepageAdvertisementPicList;
	private int mainImgoldPosition = 0;// 记录上一次点的位置

	private ViewPager viewpager_container;
	private ViewPagerAdapter mainViewPagerAdapter;

	private Activity  mContext;

	private TextView textView;

	private List<String> imageUrls;

	private int imageSize = 0;

	public HomepageBannerViewPager(Activity context, ViewPager viewpager_container,TextView textView,List<String> imageUrls) {
		this.mContext = context;
		this.viewpager_container = viewpager_container;
		this.textView = textView;
		this.imageUrls = imageUrls;
	}

	// 初始化轮播图片
	@SuppressLint("SimpleDateFormat")
	@SuppressWarnings("deprecation")
	public void initScrollView() {
		homepageAdvertisementPicList = new ArrayList<View>();
		imageSize = imageUrls.size();
		for (int i = 0; i < imageSize; i++){
			SimpleDraweeView iv_homepage_advertisement_pic = new SimpleDraweeView(mContext);
			Uri uri = Uri.parse(imageUrls.get(i));
			iv_homepage_advertisement_pic.setImageURI(uri);
			homepageAdvertisementPicList.add(iv_homepage_advertisement_pic);
		}
		textView.setText("1/"+imageSize);
		// 图片轮播的viewpager
		mainViewPagerAdapter = new ViewPagerAdapter();
		viewpager_container.setAdapter(mainViewPagerAdapter);
		viewpager_container.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				mainImgoldPosition = position;
				textView.setText(position + 1 + "/"+imageSize);
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

		// 图片自动播放所延迟的时间
		Timer timer = new Timer();
		TimerTask task = new TimerTask() {

			@Override
			public void run() {
				Message msg = new Message();
				msg.what = 0;
				handler.sendMessage(msg);
			}
		};
		// 重复播放 每隔两秒 发送
		timer.schedule(task, 4000, 3000);
	}



	// 加载视图的Adapter
	private class ViewPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return homepageAdvertisementPicList.size();
		}

		// 是否是同一张图片
		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		// 移除
		@Override
		public void destroyItem(ViewGroup view, int position, Object object) {
			view.removeView(homepageAdvertisementPicList.get(position));

		}

		// 添加
		@Override
		public Object instantiateItem(ViewGroup view, int position) {
			view.addView(homepageAdvertisementPicList.get(position));
			return homepageAdvertisementPicList.get(position);
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// 循环滚动
			if (msg.what == 0) {
				// 设置当前页面 代表最后一张图片
				if (viewpager_container.getCurrentItem() == homepageAdvertisementPicList.size() - 1) {
					// 指的是不平滑过度
					viewpager_container.setCurrentItem(0, false);
				} else {
					// 默认为平滑过度
					viewpager_container.setCurrentItem(viewpager_container.getCurrentItem() + 1);
				}
			}
			super.handleMessage(msg);
		}
	};

}
